﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using RegexLib.Web.Models.Data;

namespace RegexLib.Web.Models.Web
{
	/// <summary>
	/// 用于生成和处理系统中的URL。
	/// </summary>
	public class RxUrl
	{
		/// <summary>
		/// 获取站点根路径。
		/// </summary>
		/// <returns></returns>
		public static string GetHostUrl()
		{
			StringBuilder sbUrl = new StringBuilder();
			Uri requrl = HttpContext.Current.Request.Url;

			if (requrl.Port == 80 || requrl.Port == -1)
				sbUrl.AppendFormat("http://{0}", requrl.Host);
			else
				sbUrl.AppendFormat("http://{0}:{1}", requrl.Host, requrl.Port);

			sbUrl.Append(HttpContext.Current.Request.ApplicationPath);

			if (sbUrl[sbUrl.Length - 1] != '/')
				sbUrl.Append("/");

			return sbUrl.ToString();
		}

		/// <summary>
		/// 获取某个路径的完整路径（绝对路径）。
		/// </summary>
		/// <param name="path">相对于应用程序的根路径，前面无需加“~/”。</param>
		/// <returns>path的完整路径。</returns>
		public static string GetFullUrl(string path)
		{
			return GetHostUrl() + path;
		}

		/// <summary>
		/// 获取用户注册验证时的URL。
		/// </summary>
		/// <param name="email">待验证用户的Email。</param>
		/// <param name="verifyCode">验证码。</param>
		/// <returns>用于验证用户的URL地址。</returns>
		public static string GetRegisterUrl(string email, Guid verifyCode)
		{
			StringBuilder sbUrl = new StringBuilder(GetHostUrl());
			sbUrl.AppendFormat("Member/RegisterInfo/?m={0}&c={1}",
				HttpContext.Current.Server.UrlEncode(email),
				HttpContext.Current.Server.UrlEncode(verifyCode.ToString("N")));

			return sbUrl.ToString();
		}

		/// <summary>
		/// 获取用户修改密码时的URL。
		/// </summary>
		/// <param name="userId">待修改密码的用户ID。</param>
		/// <param name="verifyCode">验证码。</param>
		/// <returns>用于修改密码的URL地址。</returns>
		public static string GetChangePasswordUrl(Guid userId, Guid verifyCode)
		{
			StringBuilder sbUrl = new StringBuilder(GetHostUrl());
			sbUrl.AppendFormat("Member/ResetPassword/?u={0}&c={1}",
				HttpUtility.UrlEncode(userId.ToString("N")),
				HttpUtility.UrlEncode(verifyCode.ToString("N")));

			return sbUrl.ToString();
		}

		/// <summary>
		/// 获取用户“个人信息”页面地址。
		/// </summary>
		/// <param name="accessKey">用户的访问路径。</param>
		/// <returns></returns>
		public static string GetMemberProfileUrl(string accessKey)
		{
			return string.Format("/Member/Profile/{0}/", accessKey);
		}
		
		/// <summary>
		/// 获取编辑“个人信息”的页面地址。
		/// </summary>
		/// <param name="accessKey">用户的访问路径。</param>
		/// <returns></returns>
		public static string GetEditProfileUrl(string accessKey)
		{
			return string.Format("/Member/EditProfile/?id={0}", accessKey);
		}

		/// <summary>
		/// 获取正则表达式查看页地址。
		/// </summary>
		/// <param name="accessKey">正则表达式的访问路径。</param>
		/// <returns></returns>
		public static string GetViewRegexUrl(string accessKey)
		{
			return string.Format("/ViewRegex/{0}/", accessKey);
		}

		/// <summary>
		/// 获取“全部正则表达式”列表URL。
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public static string GetRegexListAllUrl(int pageIndex, int pageSize)
		{
			return string.Format("/Regex/All/{0}/{1}/", pageIndex, pageSize);
		}

		/// <summary>
		/// 获取查看关键字的页面地址。
		/// </summary>
		/// <param name="text">关键字文本。</param>
		/// <returns></returns>
		public static string GetViewKeywordUrl(string text)
		{
			return string.Format("/Keyword/{0}/", HttpUtility.UrlEncode(text));
		}

		/// <summary>
		/// 获取查看关键字的页面地址。
		/// </summary>
		/// <param name="text">关键字文本。</param>
		/// <param name="pageIndex">第几页。</param>
		/// <param name="pageSize">每页几个。</param>
		/// <returns></returns>
		public static string GetViewKeywordUrl(string text, int pageIndex, int pageSize)
		{
			return string.Format("/Keyword/{0}/{1}/{2}/", HttpUtility.UrlEncode(text), pageIndex, pageSize);
		}

		/// <summary>
		/// 获取用户首页地址。
		/// </summary>
		/// <param name="accessKey">用户访问路径。</param>
		/// <returns></returns>
		public static string GetUserUrl(string accessKey)
		{
			return string.Format("/User/{0}/", accessKey);
		}

		/// <summary>
		/// 获取用户首页地址。
		/// </summary>
		/// <param name="accessKey">用户访问路径。</param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public static string GetUserUrl(string accessKey, int pageIndex, int pageSize)
		{
			return string.Format("/User/{0}/{1}/{2}/", accessKey, pageIndex, pageSize);
		}

		/// <summary>
		/// 获取全部用户贡献正则表达式列表页面地址。
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public static string GetUserContributionsUrl(int pageIndex, int pageSize)
		{
			return string.Format("/UserContributions/{0}/{1}/", pageIndex, pageSize);
		}
	}
}
